package com.synergy.stockflow.sales.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.entity.SysUser;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.sales.entity.Quotation;
import com.synergy.stockflow.sales.entity.QuoteDetails;
import com.synergy.stockflow.sales.entity.ShipmentHistory;
import com.synergy.stockflow.sales.entity.ShippingDetails;
import com.synergy.stockflow.sales.service.QuotationService;
import com.synergy.stockflow.sales.service.QuoteDetailsService;
import com.synergy.stockflow.sales.service.ShipmentHistoryService;
import com.synergy.stockflow.sales.service.ShippingDetailsService;
import com.synergy.stockflow.service.SysUserService;
import com.synergy.stockflow.warehouse.entity.InventoryTransaction;
import com.synergy.stockflow.warehouse.service.InventoryTransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ShipmentHistory")
public class ShipmentHistoryController {

    @Autowired
    ShipmentHistoryService shipmentHistoryService;
    @Autowired
    ShippingDetailsService shippingDetailsService;
    @Autowired
    QuotationService quotationService;
    @Autowired
    SysUserService sysUserService;
    @Autowired
    QuoteDetailsService quoteDetailsService;
    @Autowired
    InventoryTransactionService inventoryTransactionService;

    @GetMapping
    public Result<Page<ShipmentHistory>> getShipmentHistory(
            @RequestParam(name = "current", defaultValue = "1") int current,
            @RequestParam(name = "size", defaultValue = "5") int size,
            @RequestParam(name = "shipmentHistoryId", required = false) String shipmentHistoryId,
            @RequestParam(name = "shipmentHistoryQuotationId", required = false) String shipmentHistoryQuotationId,
            @RequestParam(name = "shipmentHistoryOutboundWarehouse", required = false) String shipmentHistoryOutboundWarehouse) {
        Page<ShipmentHistory> page = new Page<>(current, size);
        page = shipmentHistoryService.select(page,current,size,shipmentHistoryId,shipmentHistoryQuotationId,shipmentHistoryOutboundWarehouse);
        for(ShipmentHistory s:page.getRecords()){
            LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
            s.setNickname(sysUserService.getOne(queryWrapper.eq(SysUser::getId,s.getShipmentHistoryOutboundClerk())).getNickname());
        }
        return Result.success(page);
    }

    @PostMapping
    public Result insert(@RequestBody ShipmentHistory shipmentHistory) {
        shipmentHistoryService.save(shipmentHistory);
        List<ShipmentHistory> list = shipmentHistoryService.list();
        for(ShippingDetails s:shipmentHistory.getShippingDetailsList()){
            s.setShippingDetailsShipmentHistoryId(list.get(list.size()-1).getShipmentHistoryId());
            s.setShippingDetailsId("SD-"+String.valueOf(new Date().getTime()));
            if(s.getShippingDetailsOutboundShipments()!=null && s.getShippingDetailsOutboundShipments().doubleValue()!=0.00){
                shippingDetailsService.save(s);
                String warehouseId = quoteDetailsService.selectWa(s.getShippingDetailsQuoteDetailsId()).getQuoteDetailsWarehouse();
                String sourceDocId=list.get(list.size()-1).getShipmentHistoryId();
                double v = s.getShippingDetailsOutboundShipments().doubleValue() - (s.getShippingDetailsOutboundShipments().doubleValue() * 2);
                InventoryTransaction inventoryTransaction = new InventoryTransaction(null,"PO-IN",sourceDocId,"shipment_history",warehouseId,s.getShippingDetailsProductsId(),s.getShippingDetailsId(),new BigDecimal(v),new Date(),"1","销售出库",null,null);
                inventoryTransactionService.save(inventoryTransaction);
            }
        }
        quotationService.updateQuotationState(shipmentHistory.getShipmentHistoryQuotationId(),"进行中");
        return Result.success(shipmentHistory);
    }

    @PutMapping
    public Result update(@RequestBody ShipmentHistory shipmentHistory){
        shipmentHistoryService.updateById(shipmentHistory);
        return Result.success(shipmentHistory);
    }

    @DeleteMapping
    public Result delete(@RequestBody List<ShipmentHistory> shipmentHistory){
        for(ShipmentHistory s:shipmentHistory){
            shipmentHistoryService.removeById(s);
            List<String> ids = s.getShippingDetailsList().stream().map(ShippingDetails::getShippingDetailsId).collect(Collectors.toList());
            shippingDetailsService.removeBatchByIds(ids);
        }

        return Result.success(shipmentHistory);
    }
}
